local GameUtils = {}
local Vector3 = require("utils.Vector3")
local gameUtils = CS.GameUtils

function GameUtils.instantiate(object, parent)
    local gameObject = CS.UnityEngine.GameObject.Instantiate(object, parent)
    local luaBehaviour = gameObject:GetComponent(typeof(CS.LuaBehaviour))
    local luaScript = nil
    if luaBehaviour then
        luaScript = luaBehaviour:GetScriptEnv()
    end
    return gameObject, luaScript
end

-- 实例化一个空对象
function GameUtils.InstantiateEmptyObject(name)
    return CS.UnityEngine.GameObject(name)
end

function GameUtils.destroy(object, delay)
    if delay and delay > 0 then
        CS.UnityEngine.GameObject.Destroy(object, delay)
    else
        CS.UnityEngine.GameObject.Destroy(object)
    end
end

function GameUtils.getMainCamera()
    return CS.UnityEngine.Camera.main
end

function GameUtils.getAnimationLength(animator, clip)
    if nil == animator or nil == animator.runtimeAnimatorController then
        return 0
    end
    local ac = animator.runtimeAnimatorController
    local tAnimationClips = ac.animationClips
    if (nil == tAnimationClips or tAnimationClips.Length <= 0) then
        return 0
    end
    local tAnimationClip = nil
    local tLen = tAnimationClips.Length
    for tCounter = 0, tLen - 1, 1 do
        tAnimationClip = tAnimationClips[tCounter]
        if nil ~= tAnimationClip and tAnimationClip.name == clip then
            return tAnimationClip.length
        end
    end
    return 0
end

function GameUtils.addChildTo(parentTransform, childTransform)
    childTransform:SetParent(parentTransform)
end

function GameUtils.instantiateTo(parentTransform, object)
    local gameObject, luaScript = GameUtils.instantiate(object)
    GameUtils.addChildTo(parentTransform, gameObject.transform)
    return gameObject, luaScript
end

function GameUtils.createCSArray(type, nums)
    return CS.System.Array.CreateInstanse(type, nums)
end

function GameUtils.dontDestroyOnLoad(gameObject)
    CS.UnityEngine.GameObject.DontDestroyOnLoad(gameObject)
end

function GameUtils.createRenderCamera(rawImage, parentTransform, layerName)
    local cameraObj = CS.UnityEngine.GameObject()
    local camera = cameraObj:AddComponent(typeof(CS.UnityEngine.Camera))
    local light = cameraObj:AddComponent(typeof(CS.UnityEngine.Light))
    light.type = 2
    light.intensity = 1
    camera.clearFlags = CS.UnityEngine.CameraClearFlags.SolidColor
    cameraObj.transform:SetParent(parentTransform)
    cameraObj.transform.localPosition = Vector3.new(0, 1.3, 2.2)
    cameraObj.transform.eulerAngles = Vector3.new(9, 180, 0)
    camera.targetTexture = CS.UnityEngine.RenderTexture(200, 340, 4)
    camera.cullingMask = CS.UnityEngine.LayerMask.GetMask(layerName)
    camera.orthographic = false
    if rawImage then
        rawImage.texture = camera.targetTexture
    end
    return cameraObj
end

function GameUtils.changeGameObjectLayer(obj, layerValue)
    obj.layer = layerValue
    local count = obj.transform.childCount
    for i = 0, count - 1 do
        local trans = obj.transform:GetChild(i)
        GameUtils.changeGameObjectLayer(trans.gameObject, layerValue)
    end
end

function GameUtils.createUISpine(rawImage, spineName, spinePos)
    local object = G_ResourceManager:loadActor(spineName)
    local gameObject = GameUtils.instantiate(object)
    gameObject.transform.position = spinePos
    GameUtils.changeGameObjectLayer(gameObject, CS.UnityEngine.LayerMask.NameToLayer("ShowModel"))
    GameUtils.createRenderCamera(rawImage, gameObject.transform, "ShowModel")
    return gameObject
end

function GameUtils.find(name)
    return CS.UnityEngine.GameObject.Find(name)
end

function GameUtils.randomRadius(radius)
    local value = math.random(0, radius)
    local symbol = math.random(1, 2)
    if symbol == 2 then
        value = -value
    end
    return value
end

function GameUtils.readFileText(filePath)
    return gameUtils.ReadFileText(filePath)
end

function GameUtils.writeFileText(filePath, text)
    gameUtils.WriteFileText(filePath, text)
end

function GameUtils.convertWorldPosToUIPos(worldPos, rect)
    local uicamera = G_UIManager:getUICamera()
    return gameUtils.convertWorldPosToUIPos(uicamera, worldPos, rect)
end

function GameUtils.converScreenPosToUIPos(screenPos, rect)
    local uicamera = G_UIManager:getUICamera()
    return gameUtils.convertScreenPointToUIPos(uicamera, screenPos, rect)
end

function GameUtils.isNull(object)
    return gameUtils.IsNull(object)
end

function GameUtils.UIWorldScale()
    return Vector3.new(0.02, 0.02, 0.02)
end

function GameUtils.getFirstPickGameObject(pos)
    return gameUtils.GetFirstPickGameObject(pos)
end

return GameUtils
